/**
 * Copyright(c) by JNet Smartware Group.
 * All rights reserved. Not distribution or redistribution without
 * any permission of JNet Smartware Group.
 * 
 * @author thuc
 * 2010/03/15
 * Program: SWTUserRegisterEntry.java
 */
package vn.smartware.co.client.admin;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.DateTime;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;
import org.eclipse.wb.swt.SWTResourceManager;

import vn.smartware.co.commons.Const;
import vn.smartware.co.commons.SWTUtils;
import vn.smartware.co.framework.bean.AddressDTO;
import vn.smartware.co.framework.bean.UserDTO;
import vn.smartware.co.framework.common.BaseConstant;
import vn.smartware.co.framework.exception.ClientException;
import vn.smartware.co.framework.exception.FatalException;
import vn.smartware.co.framework.exception.ServerException;
import vn.smartware.co.framework.exception.SessionTimeOutException;
import vn.smartware.co.framework.form.BaseForm;
import vn.smartware.co.framework.plugin.LangResources;
import vn.smartware.co.framework.swt.base.JNetComposite;
import vn.smartware.co.framework.utils.MsgBoxUtils;
import vn.smartware.co.java.net.commons.Utils;

/**
 * SWTUserRegisterEntry
 * @author thuctt
 */
public class SWTUserRegisterEntry extends JNetComposite {

	private static Log log = LogFactory.getLog(SWTUserRegisterEntry.class);
	
	private Button btnSave;

	private Button btnClear;

	private Combo cmbJob;

	private Text txtName;

	private Text txtPassword;

	private Text txtAccount;

	private CLabel clblUserId;

	private Table tblInfo;

	private Button btnSearch;

	private Text txtIDEnd;

	private Text txtIDStart;

	private Button btnEdit;

	private Button btnClose;

	private Button btnNew;

	private Button btnDelete;
	
	private Group grpDetail;
	
	private Group grpUserInfo;
	
	private Group grpSearch;

	private Text txtCity;

	private Text txtDistrict;

	private Text txtWard;

	private Text txtStreet;

	private Text txtHouseNo;

	private Text txtIDNumber;
	
	private Text txtTel;

	private CLabel clblMessage;

	private Text txtEmail;

	private CLabel clblStatus;

	private Button btnApply;

	private Button chkDetail;

	private Button chkAccountLocked;
	
	private static final int TBL_ROW_COUNT = 7;
	
	private String mode;
	
	private UserDTO tmpUser;
	
	private int editingIndex = -1;
	
	private int dataRowCount = 0;
	
	private DateTime txtBirthday;
	
	private boolean alreadySearchFlag = false;
	
	/**
	 * Constructor.
	 * @param parent 
	 * @param style 
	 * @param prgName 
	 */
	public SWTUserRegisterEntry(Composite parent, int style, String prgName) {
		super(parent, style, prgName);
	}
	
	/**
	 * init data to display
	 * @throws FatalException
	 */
	protected boolean loadData() throws FatalException {
		Map<Object, Object> map = new HashMap<Object, Object>();
		map.put(Const.COMBO_KEY.KEY, Const.SYSTEM_CONFIG.JOB);
		map.put(Const.COMBO_KEY.HAS_BLANK, Boolean.TRUE);
		SWTUtils.ComboProcess.setComboItems(map, cmbJob, LangResources.getText("SWTUserRegisterEntry.lblJob"));
		
		return true;
	}

	/**
	 * init controls
	 */
	protected void initialize() {
		grpSearch = new Group(this, SWT.NONE);
		grpSearch.setSize(782, 61);
		grpSearch.setLocation(15, 15);
		grpSearch.setBackground(BaseConstant.COLOR_BGR_BASE);
		
		final CLabel lblID1 = new CLabel(grpSearch, SWT.CENTER | SWT.BORDER);
		lblID1.setBounds(30, 25, 100, 22);
		lblID1.setFont(BaseConstant.FONT_BASE_LABEL_NAME);
		lblID1.setText(LangResources.getText("SWTUserRegisterEntry.lblID"));

		txtIDStart = new Text(grpSearch, SWT.BORDER | SWT.RIGHT);
		txtIDStart.setBounds(136, 25, 120, 22);
		txtIDStart.setTextLimit(30);
		txtIDStart.setFont(BaseConstant.FONT_BASE_TEXT);
		txtIDStart.setBackground(BaseConstant.COLOR_TEXT_NORMAL);

		final Label lbl1 = new Label(grpSearch, SWT.CENTER);
		lbl1.setText("-");
		lbl1.setBounds(288, 28, 22, 18);
		lbl1.setFont(BaseConstant.FONT_BASE_LABEL);
		lbl1.setBackground(BaseConstant.COLOR_BGR_BASE);

		txtIDEnd = new Text(grpSearch, SWT.BORDER | SWT.RIGHT);
		txtIDEnd.setBounds(343, 25, 120, 22);
		txtIDEnd.setTextLimit(30);
		txtIDEnd.setTabs(2);
		txtIDEnd.setFont(BaseConstant.FONT_BASE_TEXT);
		txtIDEnd.setBackground(BaseConstant.COLOR_TEXT_NORMAL);
		
		btnSearch = new Button(grpSearch, SWT.NONE);
		btnSearch.setText(LangResources.getText("btnSearch"));
		btnSearch.setBounds(652, 25, 100, 23);
		btnSearch.setFont(BaseConstant.FONT_BASE_BUTTON);
		btnSearch.setCursor(SWTResourceManager.getCursor(SWT.CURSOR_HAND));
		btnSearch.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				clblMessage.setText("");
				searchAction();
			}
		});
		
		final Label line1 = new Label(this, SWT.SEPARATOR | SWT.HORIZONTAL);
		line1.setBounds(0, 82, 812, 15);
		line1.setBackground(BaseConstant.COLOR_BGR_BASE);

		tblInfo = new Table(this, SWT.FULL_SELECTION | SWT.BORDER);
		tblInfo.addSelectionListener(new SelectionAdapter() {
			public void widgetDefaultSelected(final SelectionEvent arg0) {
				if (!alreadySearchFlag) {
					return;
				}
				clblMessage.setText("");
				TableItem item = tblInfo.getItem(tblInfo.getSelectionIndex());
				Object data = item.getData();
				if (null == data){
					newAction();					
				}else{
					editAction();
				}
			}
		});
		tblInfo.setLinesVisible(true);
		tblInfo.setHeaderVisible(true);
		tblInfo.setBounds(15, 103, 782, 186);
		tblInfo.setFont(BaseConstant.FONT_BASE_LABEL);
		tblInfo.setData(BaseConstant.TABLE_ROW_COUNT, Integer.toString(TBL_ROW_COUNT));

		final TableColumn tcolHidden = new TableColumn(tblInfo, SWT.NONE);
		tcolHidden.setWidth(0);
		
		final TableColumn tcolUserID = new TableColumn(tblInfo, SWT.NONE);
		tcolUserID.setAlignment(SWT.CENTER);
		tcolUserID.setWidth(65);
		tcolUserID.setText(LangResources.getText("SWTUserRegisterEntry.tcolUserID"));
		
		final TableColumn tcolName = new TableColumn(tblInfo, SWT.LEFT);
		tcolName.setWidth(200);
		tcolName.setText(LangResources.getText("SWTUserRegisterEntry.tcolName"));
		
		final TableColumn tcolAccount = new TableColumn(tblInfo, SWT.LEFT);
		tcolAccount.setWidth(100);
		tcolAccount.setText(LangResources.getText("SWTUserRegisterEntry.tcolAccount"));
		
		final TableColumn tcolPass = new TableColumn(tblInfo, SWT.LEFT);
		tcolPass.setWidth(100);
		tcolPass.setText(LangResources.getText("SWTUserRegisterEntry.tcolPass"));
		
		final TableColumn tcolAccLockYmd = new TableColumn(tblInfo, SWT.CENTER);
		tcolAccLockYmd.setWidth(100);
		tcolAccLockYmd.setText(LangResources.getText("SWTUserRegisterEntry.tcolAccLockYmd"));
		
		final TableColumn tcolMail = new TableColumn(tblInfo, SWT.LEFT);
		tcolMail.setWidth(212);
		tcolMail.setText(LangResources.getText("SWTUserRegisterEntry.tcolMail"));
		
		final Label line2 = new Label(this, SWT.HORIZONTAL | SWT.SEPARATOR);
		line2.setBounds(0, 324, 812, 15);
		line2.setBackground(BaseConstant.COLOR_BGR_BASE);
		
		btnDelete = new Button(this, SWT.NONE);
		btnDelete.setBounds(485, 295,100, 23);
		btnDelete.setFont(BaseConstant.FONT_BASE_BUTTON);
		btnDelete.setText(LangResources.getText("btnDelete"));
		btnDelete.setCursor(SWTResourceManager.getCursor(SWT.CURSOR_HAND));
		btnDelete.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				deleteAction();
			}
		});

		btnNew = new Button(this, SWT.NONE);
		btnNew.setBounds(591, 295,100, 23);
		btnNew.setFont(BaseConstant.FONT_BASE_BUTTON);
		btnNew.setText(LangResources.getText("btnNew"));
		btnNew.setCursor(SWTResourceManager.getCursor(SWT.CURSOR_HAND));
		btnNew.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				newAction();
			}
		});

		btnEdit = new Button(this, SWT.NONE);
		btnEdit.setBounds(697, 295,100, 23);
		btnEdit.setFont(BaseConstant.FONT_BASE_BUTTON);
		btnEdit.setText(LangResources.getText("btnEdit"));
		btnEdit.setCursor(SWTResourceManager.getCursor(SWT.CURSOR_HAND));
		btnEdit.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				editAction();
			}
		});
		
		grpDetail = new Group(this, SWT.NONE);
		grpDetail.setSize(782, 264);
		grpDetail.setLocation(15, 334);
		grpDetail.setBackground(BaseConstant.COLOR_BGR_BASE);
		
		clblStatus = new CLabel(grpDetail, SWT.RIGHT | SWT.SHADOW_IN | SWT.BORDER);
		clblStatus.setBounds(10, 25,100, 22);
		clblStatus.setForeground(BaseConstant.COLOR_LINK_LABEL);
		clblStatus.setFont(BaseConstant.FONT_BOLD_LABEL);
		clblStatus.setBackground(BaseConstant.COLOR_BGR_LABEL);
		clblStatus.setAlignment(SWT.CENTER);

		final CLabel lblUserId = new CLabel(grpDetail, SWT.BORDER | SWT.CENTER);
		lblUserId.setBounds(10, 72, 80, 22);
		lblUserId.setFont(BaseConstant.FONT_BASE_LABEL_NAME);
		lblUserId.setText(LangResources.getText("SWTUserRegisterEntry.lblUserId"));

		clblUserId = new CLabel(grpDetail, SWT.RIGHT | SWT.SHADOW_IN | SWT.BORDER);
		clblUserId.setAlignment(SWT.RIGHT);
		clblUserId.setBackground(BaseConstant.COLOR_BGR_LABEL);
		clblUserId.setFont(BaseConstant.FONT_BASE_LABEL);
		clblUserId.setBounds(96, 72, 122, 22);

		final CLabel lblName = new CLabel(grpDetail, SWT.BORDER | SWT.CENTER);
		lblName.setBounds(10, 102, 80, 22);
		lblName.setFont(BaseConstant.FONT_BASE_LABEL_NAME);
		lblName.setText(LangResources.getText("SWTUserRegisterEntry.lblName"));

		txtName = new Text(grpDetail, SWT.BORDER);
		txtName.setBounds(96, 102, 244, 22);
		txtName.setTextLimit(50);
		txtName.setFont(BaseConstant.FONT_BASE_TEXT);
		txtName.setBackground(BaseConstant.COLOR_TEXT_REQUIRED);

		final CLabel lblAccount = new CLabel(grpDetail, SWT.BORDER | SWT.CENTER);
		lblAccount.setBounds(10, 130, 80, 22);
		lblAccount.setFont(BaseConstant.FONT_BASE_LABEL_NAME);
		lblAccount.setText(LangResources.getText("SWTUserRegisterEntry.lblAccount"));

		txtAccount = new Text(grpDetail, SWT.BORDER);
		txtAccount.setBounds(96, 130, 122, 22);
		txtAccount.setTextLimit(20);
		txtAccount.setFont(BaseConstant.FONT_BASE_TEXT);
		txtAccount.setBackground(BaseConstant.COLOR_TEXT_REQUIRED);

		chkAccountLocked = new Button(grpDetail, SWT.CHECK);
		chkAccountLocked.setText(LangResources.getText("SWTUserRegisterEntry.chkAccountLocked"));
		chkAccountLocked.setBounds(236, 133, 104, 16);
		chkAccountLocked.setBackground(BaseConstant.COLOR_BGR_BASE);

		final CLabel lblPassword = new CLabel(grpDetail, SWT.BORDER | SWT.CENTER);
		lblPassword.setBounds(10, 158, 80, 22);
		lblPassword.setFont(BaseConstant.FONT_BASE_LABEL_NAME);
		lblPassword.setText(LangResources.getText("SWTUserRegisterEntry.lblPassword"));

		txtPassword = new Text(grpDetail, SWT.BORDER);
		txtPassword.setBounds(96, 158, 122, 22);
		txtPassword.setTextLimit(20);
		txtPassword.setFont(BaseConstant.FONT_BASE_TEXT);
		txtPassword.setBackground(BaseConstant.COLOR_TEXT_REQUIRED);
		
		final CLabel lblEmail = new CLabel(grpDetail, SWT.CENTER | SWT.BORDER);
		lblEmail.setBounds(10, 186, 80, 22);
		lblEmail.setFont(BaseConstant.FONT_BASE_LABEL_NAME);
		lblEmail.setText(LangResources.getText("SWTUserRegisterEntry.lblEmail"));
		
		txtEmail = new Text(grpDetail, SWT.BORDER);
		txtEmail.setBounds(96, 186, 244, 22);
		txtEmail.setTextLimit(30);
		txtEmail.setFont(BaseConstant.FONT_BASE_TEXT);
		txtEmail.setBackground(BaseConstant.COLOR_TEXT_NORMAL);
		
		chkDetail = new Button(grpDetail, SWT.CHECK);
		chkDetail.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent arg0) {
				if (chkDetail.getSelection()){
					SWTUtils.OperatorUI.setEnableAll(grpUserInfo.getChildren(), true);
				}else{
					SWTUtils.OperatorUI.setEnableAll(grpUserInfo.getChildren(), false);
				}
				txtIDNumber.setFocus();
			}
		});
		chkDetail.setBounds(346, 25, 93, 16);
		chkDetail.setText(LangResources.getText("SWTUserRegisterEntry.chkDetail"));
		chkDetail.setBackground(BaseConstant.COLOR_BGR_BASE);
		
		grpUserInfo = new Group(grpDetail, SWT.NONE);
		grpUserInfo.setText(LangResources.getText("SWTUserRegisterEntry.grpUserInfo"));
		grpUserInfo.setSize(426, 207);
		grpUserInfo.setLocation(346, 47);
		grpUserInfo.setBackground(BaseConstant.COLOR_BGR_BASE);
		
		final CLabel lblIDNumber = new CLabel(grpUserInfo, SWT.CENTER | SWT.BORDER);
		lblIDNumber.setBounds(10, 25, 80, 22);
		lblIDNumber.setFont(BaseConstant.FONT_BASE_LABEL);
		lblIDNumber.setText(LangResources.getText("SWTUserRegisterEntry.lblIDNumber"));
		
		txtIDNumber = new Text(grpUserInfo, SWT.BORDER);
		txtIDNumber.setBounds(96, 25, 206, 22);
		txtIDNumber.setTextLimit(15);
		txtIDNumber.setFont(BaseConstant.FONT_BASE_TEXT);
		txtIDNumber.setBackground(BaseConstant.COLOR_TEXT_REQUIRED);
		
		final CLabel lblBirthday = new CLabel(grpUserInfo, SWT.CENTER | SWT.BORDER);
		lblBirthday.setBounds(10, 53, 80, 22);
		lblBirthday.setFont(BaseConstant.FONT_BASE_LABEL);
		lblBirthday.setText(LangResources.getText("SWTUserRegisterEntry.lblBirthday"));
		
		txtBirthday = new DateTime(this.grpUserInfo, SWT.BORDER | SWT.DROP_DOWN);
		SWTUtils.DateTimeProcess.setDefaultDate(txtBirthday);
		txtBirthday.setBounds(96, 53, 100, 24);
				
		final CLabel lblJob = new CLabel(grpUserInfo, SWT.CENTER | SWT.BORDER);
		lblJob.setBounds(10, 83, 80, 22);
		lblJob.setFont(BaseConstant.FONT_BASE_LABEL);
		lblJob.setText(LangResources.getText("SWTUserRegisterEntry.lblJob"));

		cmbJob = new Combo(grpUserInfo, SWT.READ_ONLY);
		cmbJob.setBounds(96, 81,206, 24);
		cmbJob.setTextLimit(200);
		cmbJob.setFont(BaseConstant.FONT_BASE_TEXT);
		
		final CLabel lblTel = new CLabel(this.grpUserInfo, SWT.BORDER | SWT.CENTER);
		lblTel.setText(LangResources.getText("SWTUserRegisterEntry.lblTel"));
		lblTel.setFont(SWTResourceManager.getFont("Microsoft Sans Serif", 10, SWT.NORMAL));
		lblTel.setBounds(10, 111, 80, 22);
		
		this.txtTel = new Text(this.grpUserInfo, SWT.BORDER);
		this.txtTel.setFont(BaseConstant.FONT_BASE_TEXT);
		this.txtTel.setBackground(BaseConstant.COLOR_TEXT_NORMAL);
		this.txtTel.setTextLimit(15);
		this.txtTel.setBounds(96, 111, 100, 22);
		
		final CLabel lblAddress = new CLabel(grpUserInfo, SWT.CENTER | SWT.BORDER);
		lblAddress.setBounds(10, 139, 80, 22);
		lblAddress.setFont(BaseConstant.FONT_BASE_LABEL);
		lblAddress.setText(LangResources.getText("SWTUserRegisterEntry.lblAddress"));
		
		txtHouseNo = new Text(grpUserInfo, SWT.BORDER);
		txtHouseNo.setBounds(96, 139, 79, 22);
		txtHouseNo.setTextLimit(20);
		txtHouseNo.setFont(BaseConstant.FONT_BASE_TEXT);
		txtHouseNo.setBackground(BaseConstant.COLOR_TEXT_NORMAL);
		
		txtStreet = new Text(grpUserInfo, SWT.BORDER);
		txtStreet.setBounds(217, 139, 153, 22);
		txtStreet.setTextLimit(100);
		txtStreet.setFont(BaseConstant.FONT_BASE_TEXT);
		txtStreet.setBackground(BaseConstant.COLOR_TEXT_NORMAL);
		
		txtWard = new Text(grpUserInfo, SWT.BORDER);
		txtWard.setBounds(96, 167, 38, 22);
		txtWard.setTextLimit(5);
		txtWard.setFont(BaseConstant.FONT_BASE_TEXT);
		txtWard.setBackground(BaseConstant.COLOR_TEXT_NORMAL);
		
		txtDistrict = new Text(grpUserInfo, SWT.BORDER);
		txtDistrict.setBounds(197, 167, 53, 22);
		txtDistrict.setTextLimit(20);
		txtDistrict.setFont(BaseConstant.FONT_BASE_TEXT);
		txtDistrict.setBackground(BaseConstant.COLOR_TEXT_NORMAL);
		
		txtCity = new Text(grpUserInfo, SWT.BORDER);
		txtCity.setBounds(308, 167, 62, 22);
		txtCity.setTextLimit(20);
		txtCity.setFont(BaseConstant.FONT_BASE_TEXT);
		txtCity.setBackground(BaseConstant.COLOR_TEXT_NORMAL);
		
		final Label lblHouseNo = new Label(grpUserInfo, SWT.CENTER);
		lblHouseNo.setBounds(181, 143, 25, 18);
		lblHouseNo.setFont(BaseConstant.FONT_BASE_LABEL);
		lblHouseNo.setBackground(BaseConstant.COLOR_BGR_BASE);
		lblHouseNo.setText(LangResources.getText("SWTUserRegisterEntry.lblHouseNo"));
		
		final Label lblStreet = new Label(grpUserInfo, SWT.CENTER);
		lblStreet.setBounds(373, 143, 48, 18);
		lblStreet.setFont(BaseConstant.FONT_BASE_LABEL);
		lblStreet.setBackground(BaseConstant.COLOR_BGR_BASE);
		lblStreet.setText(LangResources.getText("SWTUserRegisterEntry.lblStreet"));
		
		final Label lblWard = new Label(grpUserInfo, SWT.CENTER);
		lblWard.setBounds(140, 169, 54, 18);
		lblWard.setFont(BaseConstant.FONT_BASE_LABEL);
		lblWard.setBackground(BaseConstant.COLOR_BGR_BASE);
		lblWard.setText(LangResources.getText("SWTUserRegisterEntry.lblWard"));
		
		final Label lblDistrict = new Label(grpUserInfo, SWT.CENTER);
		lblDistrict.setBounds(257, 169, 40, 18);
		lblDistrict.setFont(BaseConstant.FONT_BASE_LABEL);
		lblDistrict.setBackground(BaseConstant.COLOR_BGR_BASE);
		lblDistrict.setText(LangResources.getText("SWTUserRegisterEntry.lblDistrict"));
		
		final Label lblCity = new Label(grpUserInfo, SWT.CENTER);
		lblCity.setBounds(376, 166, 45, 18);
		lblCity.setFont(BaseConstant.FONT_BASE_LABEL);
		lblCity.setBackground(BaseConstant.COLOR_BGR_BASE);
		lblCity.setText(LangResources.getText("SWTUserRegisterEntry.lblCity"));
		
		btnApply = new Button(grpDetail, SWT.NONE);
		btnApply.setBounds(241, 231, 100, 23);
		btnApply.setFont(BaseConstant.FONT_BASE_BUTTON);
		btnApply.setEnabled(false);
		btnApply.setText(LangResources.getText("btnApply"));
		btnApply.setCursor(SWTResourceManager.getCursor(SWT.CURSOR_HAND));
		btnApply.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				applyAction();
			}
		});
		
		final Label line3 = new Label(this, SWT.HORIZONTAL | SWT.SEPARATOR);
		line3.setBounds(0, 604, 812, 15);
		line3.setBackground(BaseConstant.COLOR_BGR_BASE);
		
		clblMessage = new CLabel(this, SWT.RIGHT | SWT.SHADOW_IN | SWT.BORDER);
		clblMessage.setBounds(15, 624, 250, 25);
		clblMessage.setForeground(SWTResourceManager.getColor(0, 0, 255));
		clblMessage.setFont(SWTResourceManager.getFont("Microsoft Sans Serif", 10, SWT.BOLD));
		clblMessage.setBackground(SWTResourceManager.getColor(232, 255, 251));
		clblMessage.setAlignment(SWT.CENTER);

		btnSave = new Button(this, SWT.NONE);
		btnSave.setBounds(485, 625, 100, 23);
		btnSave.setFont(BaseConstant.FONT_BASE_BUTTON);
		btnSave.setText(LangResources.getText("btnSave"));
		btnSave.setCursor(SWTResourceManager.getCursor(SWT.CURSOR_HAND));
		btnSave.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				clblMessage.setText("");
				saveAction();
			}
		});

		btnClear = new Button(this, SWT.NONE);
		btnClear.setBounds(591, 625, 100, 23);
		btnClear.setFont(BaseConstant.FONT_BASE_BUTTON);
		btnClear.setText(LangResources.getText("btnClear"));
		btnClear.setCursor(SWTResourceManager.getCursor(SWT.CURSOR_HAND));
		btnClear.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				clearAction();
			}
		});

		btnClose = new Button(this, SWT.NONE);
		btnClose.setBounds(697, 625, 100, 23);
		btnClose.setFont(BaseConstant.FONT_BASE_BUTTON);
		btnClose.setText(LangResources.getText("btnClose"));
		btnClose.setCursor(SWTResourceManager.getCursor(SWT.CURSOR_HAND));
		btnClose.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				if (!closeAction()){
					btnClose.setFocus();
				}
			}
		});
		
		this.getShell().setDefaultButton(btnSearch);
		
	}

	/**
	 * clearAction
	 */
	protected void clearAction() {
		alreadySearchFlag = false;
		SWTUtils.OperatorUI.clearAll(this.getChildren());
		
		SWTUtils.OperatorUI.setEnableAll(grpSearch.getChildren(), true);
		Control[] userInfoGrp = new Control[]{
				tblInfo,
				btnDelete,
				btnNew,
				btnEdit
		};
		SWTUtils.OperatorUI.setEnableAll(userInfoGrp, false);
		SWTUtils.OperatorUI.setEnableAll(grpDetail.getChildren(), false);
		btnSave.setEnabled(false);
		
		this.mode = null;
		this.tmpUser = null;
		this.editingIndex = -1;
		this.dataRowCount = 0;
		
		txtIDStart.setFocus();
	}

	/**
	 * deleteAction
	 */
	protected void deleteAction() {
		
		try {
			clblMessage.setText("");
			int idx = tblInfo.getSelectionIndex();
			TableItem tblItem = tblInfo.getItem(idx);
			UserDTO user = (UserDTO)tblItem.getData();
			if (null == user){
				MsgBoxUtils.errorMsgBox("errors.NOT_SELECTED_RECORD");
				tblInfo.setFocus();
				tblInfo.showSelection();
				return;
			}
			
			//
			if (MsgBoxUtils.questionMsgBox("question.BE_SURE") == SWT.NO){
				btnDelete.setFocus();
				return;
			}
			
			//remove item
			if (user.getUserId() == -1){
				tblInfo.remove(idx);
				makeNewRow();
				tblInfo.setSelection(idx);
				btnDelete.setFocus();
				MsgBoxUtils.setStatusLabelMessage(clblMessage, "info.DELETE_SUCCESSFUL");
				return;
			}
			
			ArrayList<UserDTO> delList = new ArrayList<UserDTO>();
			delList.add(user);
			BaseForm form = new BaseForm();
			form.setUserList(delList);
			form.setActionForm("baseForm");
			form.setActionMethod("deleteUserAction");
			form = (BaseForm)execute(form);
			
			searchAction();
			MsgBoxUtils.setStatusLabelMessage(clblMessage, "info.DELETE_SUCCESSFUL");
			
		} catch (SessionTimeOutException e) {
			log.warn("SessionTimeOutException", e);
		}catch (ClientException e) {
			log.error("ClientException", e);
			fatalErrorMsgBox();
		} catch (FatalException e) {
			log.error("FatalException", e);
			fatalErrorMsgBox();
		}
	}

	/**
	 * make a new row of table
	 *
	 */
	private void makeNewRow() {

		TableItem tmp = new TableItem(tblInfo, SWT.NONE);
		if ((tblInfo.getItemCount() - 1)%2 == 0){
			tmp.setBackground(BaseConstant.COLOR_TABLE_ROW_EVEN);
		}
	}

	/**
	 * displayAction
	 */
	protected void searchAction() {
		
		try {
			// check action
			if (!Utils.isNullEmpty(txtIDStart.getText())
					&& !Utils.isNumeric(txtIDStart.getText().trim())){
				MsgBoxUtils.errorMsgBox("errors.NUMERIC_FORMAT", new Object[]{"ID"});
				txtIDStart.setFocus();
				return;
			}
			if (!Utils.isNullEmpty(txtIDEnd.getText())
					&& !Utils.isNumeric(txtIDEnd.getText().trim())){
				MsgBoxUtils.errorMsgBox("errors.NUMERIC_FORMAT", new Object[]{"ID"});
				txtIDEnd.setFocus();
				return;
			}
			if (!Utils.isNullEmpty(txtIDStart.getText())
					&& !Utils.isNullEmpty(txtIDEnd.getText())
					&& Integer.parseInt(txtIDStart.getText().trim()) > Integer.parseInt(txtIDEnd.getText().trim())){
				MsgBoxUtils.errorMsgBox("errors.NUMBER_GREATER_COMPARE", new Object[]{"ID", "ID"});
				txtIDStart.setFocus();
				return;
			}
			//
			alreadySearchFlag = true;
			BaseForm form = new BaseForm();
			form.setActionForm("baseForm");
			form.setActionMethod("getUsers");
			
			form.setIdStart(Utils.toEmpty(txtIDStart.getText()));
			form.setIdEnd(Utils.toEmpty(txtIDEnd.getText()));
			
			form = (BaseForm)execute(form);
			
			if (form.getUserList().isEmpty()){
				MsgBoxUtils.warningMsgBox("warning.NO_DATA_FOUND");
				return;
			}
			
			Control[] userInfoGrp = new Control[]{
					tblInfo,
					btnDelete,
					btnNew,
					btnEdit
			};
			SWTUtils.OperatorUI.setEnableAll(userInfoGrp, true);
			SWTUtils.OperatorUI.setEnableAll(grpDetail.getChildren(), false);
			SWTUtils.OperatorUI.setEnableAll(grpSearch.getChildren(), false);
			
			btnSave.setEnabled(false);
			
			dataRowCount = form.getUserList().size();
			displayTable(form.getUserList());
			
			tblInfo.setFocus();
			
		} catch (SessionTimeOutException e) {
			log.warn("SessionTimeOutException", e);
		} catch (ServerException e) {
			log.error("ServerException.", e);
			fatalErrorMsgBox();
		} catch (FatalException e) {
			log.error("FatalException.", e);
			fatalErrorMsgBox();
		}
	}

	/**
	 * display table program
	 * @param list
	 */
	private void displayTable(ArrayList<UserDTO> list) {

		tblInfo.removeAll();
		
		int rowcount = TBL_ROW_COUNT;
		if (list.size() >= rowcount){
			rowcount = list.size() + 2;
		}
		for (int i = 0; i < rowcount; i++) {
			TableItem tblItem = new TableItem(tblInfo, SWT.NONE);
			
			if (i < list.size()){
				UserDTO user = (UserDTO)list.get(i);
				
				String[] items = new String[tblInfo.getColumnCount()];
				int idx = 0;
				items[idx++] = "";
				items[idx++] = Integer.toString(user.getUserId());
//				items[idx++] = user.getUserCd();
				items[idx++] = user.getName();
				items[idx++] = user.getUserName();
				items[idx++] = user.getPassword();
//				items[idx++] = user.getLockoutDate();
				items[idx++] = Utils.formatDate(user.getLockoutDate(), BaseConstant.FMT_DATE_SHORT);
				items[idx++] = user.getMail();
				
				tblItem.setText(items);
				tblItem.setData(user);
			}
			
			if (i%2 == 0){
				tblItem.setBackground(BaseConstant.COLOR_TABLE_ROW_EVEN);
			}
		}
		tblInfo.setData(list);
		tblInfo.setSelection(0);
	}

	/**
	 * editAction
	 */
	protected void editAction() {
		
		this.mode = Const.MODE_EDIT;
		
		//clear text detail area
		SWTUtils.OperatorUI.clearAll(grpDetail.getChildren());
		
		//set data edit
		this.editingIndex = tblInfo.getSelectionIndex();
		TableItem item = tblInfo.getItem(this.editingIndex);
		tmpUser = (UserDTO)item.getData();
		if (null == tmpUser){
			try {
				MsgBoxUtils.errorMsgBox("errors.NOT_SELECTED_RECORD");
				return;
				
			} catch (ClientException e) {
				log.error("ClientException.", e);
				fatalErrorMsgBox();
				return;
			}
		}
		//set data row to detail group
		setDataToForm();
		
		//init control
		SWTUtils.OperatorUI.setEnableAll(grpDetail.getChildren(), true);
		SWTUtils.OperatorUI.setEnableAll(grpUserInfo.getChildren(), false);
		btnSave.setEnabled(true);
		
		txtName.setFocus();
	}

	private void setDataToForm() {

		clblStatus.setText(Const.MODE_EDIT);
		if (tmpUser.getUserId() != -1){
			clblUserId.setText(Integer.toString(tmpUser.getUserId()));
		}else{
			clblUserId.setText("");
		}
		txtName.setText(tmpUser.getName());
		txtAccount.setText(tmpUser.getUserName());
		txtPassword.setText(tmpUser.getPassword());
		txtEmail.setText(tmpUser.getMail());
		if (!Utils.isNullEmpty(tmpUser.getLockoutDate())){
			chkAccountLocked.setSelection(true);
		}else{
			chkAccountLocked.setSelection(false);
		}
		
		//group detail info
		chkDetail.setSelection(false);
		txtIDNumber.setText(tmpUser.getIdNumber());
		txtTel.setText(tmpUser.getTel());
		SWTUtils.DateTimeProcess.setDate(txtBirthday, tmpUser.getBirthday());
		SWTUtils.ComboProcess.setDisplayLabel(cmbJob, tmpUser.getJobCd());
		
		AddressDTO addr = tmpUser.getAddress();
		if (null != addr){
			txtHouseNo.setText(addr.getHouseNo());
			txtStreet.setText(addr.getStreet());
			txtWard.setText(addr.getWard());
			txtDistrict.setText(addr.getDistrict());
			txtCity.setText(addr.getCity());
		}
	}

	/**
	 * newAction
	 */
	protected void newAction() {
		
		this.mode = Const.MODE_NEW;
		
		SWTUtils.OperatorUI.clearAll(grpDetail.getChildren());
		clblStatus.setText(Const.MODE_NEW);
		
		SWTUtils.OperatorUI.setEnableAll(grpDetail.getChildren(), true);
		SWTUtils.OperatorUI.setEnableAll(grpUserInfo.getChildren(), false);
		btnSave.setEnabled(true);
		
		txtName.setFocus();
	}

	/**
	 * saveAction
	 */
	protected void saveAction() {
		
		try {
			ArrayList<UserDTO> list = new ArrayList<UserDTO>();
			TableItem[] rows = tblInfo.getItems();
			for (int i = 0; i < rows.length; i++) {
				if (rows[i].getBackground().equals(Const.COLOR_DATA_ROW_CHANGED)){
					list.add((UserDTO)rows[i].getData());
				}
			}
			
			if (list.isEmpty()){
				MsgBoxUtils.warningMsgBox("warning.NO_DATA_SAVE");
				return;
			}
			
			//lock screen
			lockScreen(true);
			
			BaseForm form = new BaseForm();
			form.setUserList(list);
			
			form.setActionForm("baseForm");
			form.setActionMethod("saveUsersAction");
			form = (BaseForm)execute(form);
			
			//unlock screen
			lockScreen(false);
			
			clearAction();
			searchAction();
			
			MsgBoxUtils.setStatusLabelMessage(clblMessage, "info.SAVE_SUCCESSFUL");
		
		} catch (SessionTimeOutException e) {
			log.warn("SessionTimeOutException.", e);
			
		} catch (FatalException e) {
			log.error("FatalException.", e);
			fatalErrorMsgBox();
		}
	}
	
	/**
	 * apply action
	 *
	 */
	protected void applyAction() {

		if (!checkData()){
			return;
		}
		
		getUserData();
		
		String[] items = new String[tblInfo.getColumnCount()];
		int idx = 1;
		if (tmpUser.getUserId() != -1){
			items[idx++] = Integer.toString(tmpUser.getUserId());
		}else{
			items[idx++] = "";
		}
//		items[idx++] = tmpUser.getUserCd();
		items[idx++] = tmpUser.getName();
		items[idx++] = tmpUser.getUserName();
		items[idx++] = tmpUser.getPassword();
		items[idx++] = Utils.formatDate(tmpUser.getLockoutDate(), "");
		items[idx++] = tmpUser.getMail();
		
		TableItem tblItem = null;
		if (this.mode.equals(Const.MODE_EDIT)){
			tblItem = tblInfo.getItem(this.editingIndex);
			
		}else if (this.mode.equals(Const.MODE_NEW)){
			if (this.dataRowCount == tblInfo.getItemCount() - 2) {//last row of table
				TableItem tmp = new TableItem(tblInfo, SWT.NONE);
				if ((tblInfo.getItemCount() - 1)%2 == 0) {
					tmp.setBackground(BaseConstant.COLOR_TABLE_ROW_EVEN);
				}
			}
			tblItem = tblInfo.getItem(this.dataRowCount);
			this.dataRowCount++;
		}
		
		tblItem.setText(items);
		tblItem.setBackground(Const.COLOR_DATA_ROW_CHANGED);
		tblItem.setData(tmpUser);
		
		this.mode = null;
		this.editingIndex = -1;
		this.tmpUser = null;
		
		SWTUtils.OperatorUI.clearAll(grpDetail.getChildren());
		SWTUtils.OperatorUI.setEnableAll(grpDetail.getChildren(), false);

		tblInfo.showSelection();
		tblInfo.setFocus();
	}

	/**
	 * get user data from screen
	 *
	 */
	private void getUserData() {

		if (this.mode.equals(Const.MODE_NEW)){
			tmpUser = new UserDTO();
		}
		tmpUser.setName(Utils.toEmpty(txtName.getText()));
		tmpUser.setUserName(Utils.toEmpty(txtAccount.getText()));
		tmpUser.setPassword(txtPassword.getText());
		if (chkAccountLocked.getSelection()){
			tmpUser.setLockoutDate(new Date());
		}
		tmpUser.setMail(Utils.toEmpty(txtEmail.getText()));
		tmpUser.setIdNumber(Utils.toEmpty(txtIDNumber.getText()));
//		tmpUser.setBirthday(Utils.toEmpty(txtBirthday.getText()));
		tmpUser.setBirthday(SWTUtils.DateTimeProcess.getDate(txtBirthday));
		tmpUser.setJobCd(SWTUtils.ComboProcess.getSelectingValue(cmbJob));
		tmpUser.setTel(Utils.toEmpty(txtTel.getText()));
		//
		AddressDTO addr = null;
		if (tmpUser.getUserId() == -1){
			addr = new AddressDTO();
		}else{
			addr = tmpUser.getAddress();
			if (addr == null){
				addr = new AddressDTO();
			}
		}
		addr.setHouseNo(Utils.toEmpty(txtHouseNo.getText()));
		addr.setStreet(Utils.toEmpty(txtStreet.getText()));
		addr.setWard(Utils.toEmpty(txtWard.getText()));
		addr.setDistrict(Utils.toEmpty(txtDistrict.getText()));
		addr.setCity(Utils.toEmpty(txtCity.getText()));
		tmpUser.setAddress(addr);
	}

	/**
	 * check data input
	 * @return boolean
	 */
	private boolean checkData() {
		
		try {
			if (!SWTUtils.Check.checkRequired(txtName, 
					LangResources.getText("SWTUserRegisterEntry.lblName"))){
				return false;
			}
			
			if (!SWTUtils.Check.checkRequired(txtAccount, 
					LangResources.getText("SWTUserRegisterEntry.lblAccount"))){
				return false;
			}
			
			if (!SWTUtils.Check.checkRequired(txtPassword, 
					LangResources.getText("SWTUserRegisterEntry.lblPassword"))){
				return false;
			}
			
			if (!Utils.isNullEmpty(txtEmail.getText())){
				if (!SWTUtils.Check.checkEmail(txtEmail, 
						LangResources.getText("SWTUserRegisterEntry.lblEmail"))){
					return false;
				}
			}
			
			if (chkDetail.getSelection()){
				if (!SWTUtils.Check.checkRequired(txtIDNumber, 
						LangResources.getText("SWTUserRegisterEntry.lblIDNumber"))){
					return false;
				}
				
				//check only Latin AlphaB, and number
				if (!SWTUtils.Check.checkNumeric(txtIDNumber, 
						LangResources.getText("SWTUserRegisterEntry.lblIDNumber"))){
					return false;
				}
			}
			
		} catch (ClientException e) {
			log.error("ClientException.", e);
			fatalErrorMsgBox();
			return false;
		}
		return true;
	}

	/**
	 * lockScreen
	 * @param flg 
	 */
	protected void lockScreen(boolean flg) {
		
		SWTUtils.OperatorUI.setEnableAll(this.getChildren(), !flg);
	}
}